package dz.common;

import hezi.core.Image;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.log4j.Logger;

public class ImageTool {
	
	public static String getWidthAndHeight(String path) {
		File file = new File(path);
		FileInputStream fis;
		try {
			fis = new FileInputStream(file);
			BufferedImage bufferedImg = ImageIO.read(fis);
			if(null == bufferedImg){
				return "0,1";
			}
			int h = bufferedImg.getHeight();
			int w = bufferedImg.getWidth();
			fis.close();
			return w+","+h;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "0,1";
	}
	

	static int getHeight(String path){
		File file = new File(path);
		FileInputStream fis;
		boolean isTooBig = false;
		try {
			fis = new FileInputStream(file);
			BufferedImage bufferedImg = ImageIO.read(fis);
			if(null == bufferedImg){
				return 0;
			}
			return bufferedImg.getHeight();
		} catch (FileNotFoundException e) {
			logger.debug(e.getMessage());
		} catch (IOException e) {
			logger.debug(e.getMessage());
		}
		return 0;
	}
	
	
	static int getWidth(String path){
		File file = new File(path);
		FileInputStream fis;
		boolean isTooBig = false;
		try {
			fis = new FileInputStream(file);
			BufferedImage bufferedImg = ImageIO.read(fis);
			if(null == bufferedImg){
				return 0;
			}
			return bufferedImg.getWidth();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			logger.debug(e.getMessage());
		}
		return 0;
	}

	
	public static Logger logger = Logger.getLogger("");


	public static String getShapeString(Image image) {
		if(0 == image.getHeight()*image.getWidth()){
			return "zero";
		}
		
		double ratio = (double)image.getWidth() / (double)image.getHeight();
		if( isAVCover(image.getWidth() , image.getHeight(), ratio) ){
			return "cover";
		}
		else{
			if(ratio > 1){
				return "short";
			}
			else{
				return "tall"+ (image.getHeight()/image.getWidth());
			}
		}
	}
	
	
	private static boolean isAVCover(long width, long height, double ratio) {
		if(ratio > 1.51){
			return false;
		}
		
		if(ratio < 1.46){
			return false;
		}
		
		if(width == 500 
				|| width == 600 
				|| width == 640 
				|| width == 700 
				|| width == 740 
				|| width == 750 
				|| width == 800 
				|| width == 977 
				|| width == 1000 	
				|| width == 1024
				|| width == 1440 
				){
			return true;
		}
		
		if(height == 500 
				|| height == 600 
				){
			return true;
		}
		
		return false;
	}
}
